/***************************************************************************
 *   Copyright (C) 2008 by Goliash   *
 *   goliash@goliash.net   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
 #include "scan_dialog.h"

SearchDialog::SearchDialog(QWidget *parent) : QDialog(parent)
{
	setupUi(this);
	setFoundTable();
	connect(searchButton, SIGNAL(clicked()), this, SLOT(search()));
	connect(foundTable, SIGNAL(doubleClicked (QModelIndex)), this, SLOT(showFile(QModelIndex)));
	connect(chosenMediumButton, SIGNAL(clicked()), this, SLOT(changePlace()));

	if(((MainWindow *)parent)->getSelectedMediumId() == -1) {
		chosenMediumButton->setDisabled(true);
	}
}

void SearchDialog::setFoundTable()
{
	QStringList labels;
	labels << tr("File name") << tr("Medium") << tr("Size") << tr("Date");

	//foundTable->setSelectionModel(QAbstractItemView::SingleSelection);
	foundTable->setColumnCount(6);
	foundTable->setHorizontalHeaderLabels(labels);
	foundTable->setColumnHidden(4, true);
	foundTable->setColumnHidden(5, true);
	foundTable->show();
}

void SearchDialog::search()
{
	//qDebug() << "searching";
	foundTable->clearContents();

	QString expr = searchExpr->text();
	
	if(!expr.isEmpty()) {
		QString whereToSearch;
		if(chosenMediumButton->isChecked()) {
			whereToSearch = QString("AND M.id='%1'").arg(where);
		}
		else {
			whereToSearch = "";
		}

		QString searchCondition;
		if(wildcardButton->isChecked()) {
			searchCondition = QString(expr);
		}
		else {
			// part of name is checked
			searchCondition = QString("*%1*").arg(expr);
		}
				
		QString caseSense;
		if(caseSensitive->isChecked()) {
			caseSense = "F.name";
		}
		else {
			caseSense = "lower(F.name)";
			searchCondition = searchCondition.toLower();
		}

		QSqlQuery query;
		query.exec(QString("SELECT T.icon, F.name, M.name, G.size, G.creation_date, F.parent_id, M.id  FROM files F, media M, general G, types T WHERE %3 GLOB '%1' AND F.id_medium=M.id AND F.id=G.id AND F.id_type=T.id_type %2").arg(searchCondition).arg(whereToSearch).arg(caseSense));

		qDebug() << query.lastError();
		int row = 0;
		while(query.next()) {
			foundTable->setRowCount(row+1);
			// name
			QTableWidgetItem *itemName = new QTableWidgetItem(query.value(1).toString());
     		itemName->setIcon(QIcon(query.value(0).toString()));
			foundTable->setItem(row, 0, itemName);

			// medium name
			QTableWidgetItem *itemMedium = new QTableWidgetItem(query.value(2).toString());
     		itemMedium->setIcon(QIcon(":/cd.xpm"));
			foundTable->setItem(row, 1, itemMedium);

			// size
			QTableWidgetItem *itemSize = new QTableWidgetItem(File::getFormattedSize(query.value(3).toDouble(), File::DynamicF));
			itemSize->setTextAlignment(Qt::AlignRight);
			itemSize->setTextAlignment(Qt::AlignVCenter);
			foundTable->setItem(row, 2, itemSize);

			// date
			QTableWidgetItem *itemDate = new QTableWidgetItem(File::getFormattedDate(query.value(4).toString(), File::StandardDateF));
			itemDate->setTextAlignment(Qt::AlignVCenter);
			foundTable->setItem(row, 3, itemDate);
			
			// parent file id
			QTableWidgetItem *itemId = new QTableWidgetItem(query.value(5).toString());
			foundTable->setItem(row, 4, itemId);

			// medium id
			QTableWidgetItem *itemMid = new QTableWidgetItem(query.value(6).toString());
			foundTable->setItem(row, 5, itemMid);
	
			row ++;
		}
		foundLabel->setText(QString(tr("Found: ")) + QString::number(row));
	}
}

void SearchDialog::showFile(QModelIndex index)
{
	this->close();

	// passing parent_id and medium id to showFolder()
	((MainWindow *)(this->parentWidget()))->showFolder(foundTable->item(index.row(),4)->text().toInt(), foundTable->item(index.row(),5)->text().toInt());
}

void SearchDialog::changePlace()
{
	if(chosenMediumButton->isChecked()) {
		where = ((MainWindow *)(this->parentWidget()))->getSelectedMediumId();
	}
	else if(wholeDbButton->isChecked()) {
		where = -1;
	}
}

